<template>
  <div id="app-container" style="margin: 10px">
    <!--条件查询表单-->
    <el-form :inline="true" class="demo-form-inline" style="margin-bottom: -10px">
      <el-form-item label="用户姓名">
        <el-input v-model="queryMessageDTO.userName" placeholder="用户姓名" size="small" clearable/>
      </el-form-item>
      <el-form-item label="消息标题">
        <el-input v-model="queryMessageDTO.title" placeholder="消息标题" size="small" clearable/>
      </el-form-item>
      <el-form-item label="消息类型">
        <el-select
          v-model="queryMessageDTO.type"
          placeholder="消息类型"
          style="width: 120px;"
          clearable
          size="small">
          <el-option label="点赞" value="0"/>
          <el-option label="回复" value="1"/>
          <el-option label="系统消息" value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item label="是否已读">
        <el-select
          v-model="queryMessageDTO.status"
          placeholder="是否已读"
          style="width: 120px;"
          clearable
          size="small">
          <el-option label="未读" value="0"/>
          <el-option label="已读" value="1"/>
        </el-select>
      </el-form-item>
      <el-form-item label="添加时间">
        <el-date-picker
          v-model="queryMessageDTO.createTimeStart"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
          clearable
          size="small" />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="queryMessageDTO.createTimeEnd"
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
          clearable
          size="small" />
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" size="small" @click="initDate()">查询</el-button>
      <el-button plain icon="el-icon-refresh" size="small" style="margin-left: 0" @click="resetData()">清空</el-button>
    </el-form>

    <el-form :inline="true">
      <router-link to="/message/add">
        <el-button type="primary" plain icon="el-icon-plus" size="small">添加消息</el-button>
      </router-link>
      <el-button type="danger" plain icon="el-icon-delete" size="small" @click="deleteBySelect">批量删除</el-button>
      <el-button type="warning" plain icon="el-icon-download" style="margin-left: 0" size="small" @click="handleExport">导出数据</el-button>
    </el-form>

    <!--数据显示列表-->
    <el-table
      :data="messageList"
      fit
      highlight-current-row
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55"
        align="center"/>
      <el-table-column label="序号" width="60" align="center">
        <template slot-scope="scope">
          {{ (currentPage-1)*pageSize + scope.$index +1 }}
        </template>
      </el-table-column>

      <el-table-column prop="userName" label="名称" width="80" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.type != '2'">{{ scope.row.userName }}</span>
          <span v-if="scope.row.type == '2'">全体用户</span>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="消息标题" width="160" align="center"/>
      <el-table-column prop="content" label="消息内容" align="center"/>
      <el-table-column label="消息类型" prop="type" width="80" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.type == '0'">点赞</span>
          <span v-if="scope.row.type == '1'">回复</span>
          <span v-if="scope.row.type == '2'">系统通知</span>
        </template>
      </el-table-column>
      <el-table-column label="是否已读" prop="status" width="80" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.status == '0'">未读</span>
          <span v-if="scope.row.status == '1'">已读</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="添加时间" width="160" align="center"/>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <router-link :to="'/message/update/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
          </router-link>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>

<script>
import message from '../../api/message'
import fileDownload from 'js-file-download'
import axios from 'axios'
export default {
  data() {
    return {
      queryMessageDTO: {
        userName: '',
        status: '',
        type: '',
        createTimeStart: '',
        createTImeEnd: ''
      },
      messageList: [],
      currentPage: 1,
      pageSize: 10,
      total: 1,
      ids: [],
      multipleSelection: ['']
    }
  },
  created() {
    this.initDate()
  },
  methods: {
    resetData() {
      this.queryMessageDTO = {}
      this.initDate()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.initDate()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
      this.initDate()
    },
    handleDelete(id) {
      this.$confirm('是否确定删除', '提示信息', { type: 'warning' }).then(() => {
        this.ids.push(id)
        message.deleteMessage(this.ids).then(response => {
          this.$message.success('删除成功')
          this.initDate()
          this.ids = []
        })
      }).catch(() => {
        this.$message.info('取消删除操作')
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = []
      for (let i = 0; i < val.length; i++) {
        if (this.multipleSelection.indexOf(val[i].id) === -1) {
          this.multipleSelection.push(val[i].id)
        }
      }
      console.log(this.multipleSelection)
    },
    deleteBySelect() {
      this.$confirm('是否确定删除', '提示信息', { type: 'warning' }).then(() => {
        message.deleteMessage(this.multipleSelection).then(response => {
          this.$message.success('删除成功')
          this.multipleSelection = []
          this.initDate()
        })
      }).catch(() => {
        this.$message.info('取消删除操作')
      })
    },
    initDate() {
      message.listPage(this.currentPage, this.pageSize, this.queryMessageDTO).then(response => {
        this.messageList = response.data.data
        this.total = response.data.total
      })
    },
    handleExport() {
      console.log('===导出数据===')
      axios({
        method: 'post',
        url: 'http://localhost:8080/message/export',
        responseType: 'blob',
        data: this.queryMessageDTO
      }).then(res => {
        if (res && res.data) {
          fileDownload(res.data, '消息.xlsx')
        }
      })
    }
  }
}
</script>
